class TestRaycast extends Test
{
    public laser:Box2D.Dynamics.b2Body;
    public constructor(sprite:DebugDraw)
    {
        super(sprite);
        this.createScene();
    }

    private createScene():void
    {
        this.m_world.SetGravity(new Box2D.Common.Math.b2Vec2(0,0));

        let ground:Box2D.Dynamics.b2Body = this.m_world.GetGroundBody();

        let box:Box2D.Collision.Shapes.b2PolygonShape = new Box2D.Collision.Shapes.b2PolygonShape();
        box.SetAsBox(30 / this.m_physScale, 4 / this.m_physScale);
        let fd:Box2D.Dynamics.b2FixtureDef = new Box2D.Dynamics.b2FixtureDef();
        fd.shape = box;
        fd.density = 4;
        fd.friction = 0.4;
        fd.restitution = 0.3;

        let bd:Box2D.Dynamics.b2BodyDef = new Box2D.Dynamics.b2BodyDef();
        bd.type = Box2D.Dynamics.b2Body.b2_dynamicBody;
        bd.position.Set(320 / this.m_physScale, 150 / this.m_physScale);

        //let laser:Box2D.Dynamics.b2Body;
        this.laser = this.m_world.CreateBody(bd);
        this.laser.CreateFixture(fd);
        this.laser.SetAngle(0.5);
        this.laser.SetAngle(Math.PI);

        let circle:Box2D.Collision.Shapes.b2CircleShape = new Box2D.Collision.Shapes.b2CircleShape( 30 / this.m_physScale);
        fd.shape = circle;
        fd.density = 4;
        fd.friction = 0.4;
        fd.restitution = 0.3;
        fd.userData = "circle";
        bd.position.Set(100 / this.m_physScale, 100 / this.m_physScale);
        let body:Box2D.Dynamics.b2Body = this.m_world.CreateBody(bd);
        body.CreateFixture(fd);


    }

    public update():void
    {
        super.update();

        let p1:Box2D.Common.Math.b2Vec2 = this.laser.GetWorldPoint( new Box2D.Common.Math.b2Vec2(30.1 / this.m_physScale, 0));
        let p2:Box2D.Common.Math.b2Vec2 = this.laser.GetWorldPoint( new Box2D.Common.Math.b2Vec2(130.1 / this.m_physScale, 0));
        
        let f:Box2D.Dynamics.b2Fixture = this.m_world.RayCastOne(p1,p2);
        let lambda:number = 1;
        if(f)
        {
            let input:Box2D.Collision.b2RayCastInput = new Box2D.Collision.b2RayCastInput(p1,p2);
            let output:Box2D.Collision.b2RayCastOutput = new Box2D.Collision.b2RayCastOutput();

            f.RayCast(output, input);
            lambda = output.fraction;
        }
        this.m_sprite.graphics.lineStyle(1,0xff0000,1);
        this.m_sprite.graphics.moveTo(p1.x * this.m_physScale, p1.y * this.m_physScale);
        this.m_sprite.graphics.lineTo((p2.x * lambda + (1- lambda) * p1.x) * this.m_physScale,
                                      (p2.y * lambda + (1- lambda) * p1.y) * this.m_physScale);

    }
}